package com.jingdianjichi.subject.application.controller;

import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectAnswerDTOConverter;
import com.jingdianjichi.subject.application.convert.SubjectInfoDTOConverter;
import com.jingdianjichi.subject.application.convert.SubjectPageTOInfoConverter;
import com.jingdianjichi.subject.application.dto.SubjectInfoDTO;
import com.jingdianjichi.subject.application.dto.SubjectPageDTO;
import com.jingdianjichi.subject.common.entity.PageResult;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.service.SubjectInfoDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectInfoEs;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import com.jingdianjichi.subject.infra.basic.service.SubjectEsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    @PostMapping("/add")
    public Result add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.add.subjectInfoDTO:{}", subjectInfoDTO);
            }

            Preconditions.checkNotNull(subjectInfoDTO.getSubjectName(), "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficulty(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds()), "题目分类id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds()), "题目标签id不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOS = SubjectAnswerDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO.getOptionList());
            subjectInfoBO.setOptionList(subjectAnswerBOS);

            Boolean add = subjectInfoDomainService.add(subjectInfoBO);
            return Result.ok();
        } catch (Exception e) {
            if (log.isInfoEnabled()) {
                log.error("SubjectController.add.error:{}", e.getMessage());
            }
            return Result.fail("添加题目失败");
        }
    }

    /**
     * 查询题目列表
     *
     * @param subjectPageDTO
     * @return
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectPageDTO subjectPageDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPage.subjectPageDTO:{}", subjectPageDTO);
            }

            Preconditions.checkNotNull(subjectPageDTO.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectPageDTO.getLabelId(), "标签id不能为空");

            SubjectInfoBO subjectInfoBO = SubjectPageTOInfoConverter.INSTANCE.convertPageToInfo(subjectPageDTO);

            PageResult<SubjectInfoBO> boPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBO);
            List<SubjectInfoDTO> subjectInfoDTOS = SubjectInfoDTOConverter.INSTANCE.convertBOToDTO(boPageResult.getResult());
            PageResult<SubjectInfoDTO> dtoPageResult = new PageResult<>();
            dtoPageResult.setPageSize(boPageResult.getPageSize());
            dtoPageResult.setPageNo(boPageResult.getPageNo());
            dtoPageResult.setRecords(subjectInfoDTOS);
            dtoPageResult.setTotal(boPageResult.getTotal());
            return Result.ok(dtoPageResult);
        } catch (Exception e) {
            if (log.isInfoEnabled()) {
                log.error("SubjectController.getSubjectPage.error:{}", e.getMessage());
            }
            return Result.fail("查询题目列表失败");
        }
    }

    /**
     * 根据题目信息查询题目
     *
     * @param subjectPageDTO
     * @return
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectPageDTO subjectPageDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPage.subjectPageDTO:{}", subjectPageDTO);
            }

            Preconditions.checkNotNull(subjectPageDTO.getId(), "题目id不能为空");
//            Preconditions.checkNotNull(subjectPageDTO.getCategoryId(), "分类id不能为空");
//            Preconditions.checkNotNull(subjectPageDTO.getLabelId(), "标签id不能为空");

            SubjectInfoBO subjectInfoBO = SubjectPageTOInfoConverter.INSTANCE.convertPageToInfo(subjectPageDTO);

            SubjectInfoBO boResult = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO subjectInfoDTO = SubjectInfoDTOConverter.INSTANCE.convertBOToDTO(boResult);
            return Result.ok(subjectInfoDTO);

        } catch (Exception e) {
            if (log.isInfoEnabled()) {
                log.error("SubjectController.querySubjectInfo.error:{}", e.getMessage());
            }
            return Result.fail("根据题目信息查询题目失败");
        }
    }

    @Resource
    private SubjectEsService subjectEsService;

    @PostMapping("querySubjectByKeyword")
    public Result<PageResult<SubjectInfoEs>> querySubjectByKeyword(){
        SubjectInfoEs subjectInfoEs = new SubjectInfoEs();
        subjectInfoEs.setKeyWord("redis");
        PageResult<SubjectInfoEs> pageResult = subjectEsService.querySubjectList(subjectInfoEs);
        return Result.ok(pageResult);
    }

}
